<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/14
 * Time: 16:05
 */

namespace app\index\model;

use think\Db;
use think\Exception;

class Product
{
    public function getID($code)
    {
        return (string)DB::table('homegarden.product')->where('product_id', $code)->value('id');
    }

    public function getReplaceProduct()
    {
        return DB::query("SELECT
                enpro.id,
                enpro.activity_id
            FROM
                (
                    SELECT
                        id,
                        `code`,
                        activity_id
                    FROM
                        homegarden_cms.product
                    WHERE
                        is_used = 1
                ) enpro
            JOIN (
                SELECT
                    id,
                    `code`
                FROM
                    (
                        SELECT
                            id,
                            `code`
                        FROM
                            homegarden_cms.product
                        WHERE
                            is_used = 0
                        AND enable_status = 0
                        AND enable_time < NOW()
                        ORDER BY
                            enable_time DESC
                    ) product
                GROUP BY
                    `code`
            ) dispro ON dispro.`code` = enpro.`code`");
    }

    public function getDisableProduct()
    {
        return DB::query("SELECT
                id,
                activity_id
            FROM
                homegarden_cms.product
            WHERE
                disable_status = 0
            AND disable_time < NOW()");
    }

    public function getEnableProduct()
    {
        return DB::query("SELECT
                id,
                activity_id
            FROM
                (
                    SELECT
                        id,
                        `code`,
                        activity_id
                    FROM
                        homegarden_cms.product
                    WHERE
                        is_used = 0
                    AND enable_status = 0
                    AND enable_time < NOW()
                    ORDER BY
                        enable_time DESC
                ) product
            GROUP BY
                `code`");
    }

    public function getProductsInfo($id_arr)
    {
        $map['id'] = ['in', $id_arr];
        return DB::table('homegarden_cms')->where($map)->column('
            `code` product_id,
            name product_name,
            price product_price,
            main_price product_mainPrice,
            standard product_standard,
            brief_description product_briefDescription,
            description product_description,
            stock product_stock,
            number product_number,
            unit product_unit,
            category_class1 product_class1,
            category_class2 product_class2,
            category_class3 product_class3,
            thumb_image product_headImage,
            head_image product_bannerImage,
            detail_image product_detailImage,
            corner_image theme_coverImage,
            detail_image theme_bottomImage,
            real_weight,
            address,
            cycle,
            reposition,
            activity_id activeId,
            is_sale issale,
            is_combo,
            is_coupon,
            is_integral,
            id
        ');
    }

    public function deleteProduct($id_arr)
    {
        $map['id'] = ['in', $id_arr];
        try {
            DB::table('homegarden.product')->where($map)->delete();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function addProduct($id_arr)
    {
        $products = $this->getProductsInfo($id_arr);
        try {
            DB::table('homegarden')->insertAll($products);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }
}